//
// Created on 2025/7/30.
//

#ifndef BLANKSCREENDETECTOR_BLANKSCREENTYPES_H
#define BLANKSCREENDETECTOR_BLANKSCREENTYPES_H

#pragma once
#include <vector>
#include <cstdint>
#include <string>

struct HSVPixel {
    double h; // 色调 (0-360)
    double s; // 饱和度 (0-100)
    double v; // 明度 (0-100)
    uint8_t a; // 透明度 (0-255)
    
    HSVPixel(double hue = 0.0, double saturation = 0.0, double value = 0.0, uint8_t alpha = 255);
};

// 像素结构
struct alignas(4) Pixel {
    uint8_t r, g, b, a;
    
    Pixel(uint8_t red = 0, uint8_t green = 0, uint8_t blue = 0, uint8_t alpha = 255);
    double getLuminance() const;
    double colorDistance(const Pixel& other) const;
    bool isBlankPixel(double tolerance) const;
    bool operator==(const Pixel& other) const;
    HSVPixel toHSV() const;
};

// 矩形区域
struct Rectangle {
    int x, y, width, height;
    Rectangle(int x = 0, int y = 0, int w = 0, int h = 0);
    int getArea() const;
    bool contains(int px, int py) const;
};

// 空白区域类型
enum class BlankType {
    WHITE_SCREEN = 0,
    BLACK_SCREEN = 1,
    SOLID_COLOR = 2,
    LOADING_FAILURE = 3,
    COMPONENT_ERROR = 4
};

// 空白区域信息
struct BlankRegion {
    Rectangle bounds;
    BlankType type;
    Pixel dominantColor;     
    HSVPixel dominantColorHSV; 
    double confidence;
    double uniformity;
    int actualPixelCount;
    std::string description;
    
    BlankRegion();
};

// 检测结果
struct DetectionResult {
    bool hasBlankScreen;
    double overallBlankRatio;
    std::vector<BlankRegion> blankRegions;
    
    struct Statistics {
        int totalWhiteRegions;
        int totalBlackRegions;
        int totalSolidColorRegions;
        int largestRegionArea;
        double averageRegionSize;
        std::vector<Pixel> dominantColors;
        std::vector<HSVPixel> dominantColorsHSV;
    } stats;
    
    std::string summary;
    
    DetectionResult();
};

// 像素图类
class PixelMap {
private:
    std::vector<Pixel> pixels;
    int width, height;
    
public:
    PixelMap(const uint8_t* pixelData, int w, int h, int bytesPerPixel);
    const Pixel& getPixel(int x, int y) const;
    int getWidth() const { return width; }
    int getHeight() const { return height; }
    int getTotalPixels() const { return width * height; }
};

#endif //BLANKSCREENDETECTOR_BLANKSCREENTYPES_H
